<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
class Api_TourController extends Zend_Rest_Controller
{
	public function init()
	{
		$this->_helper->viewRenderer->setNoRender(true);
	}


	public function deleteAction() {
            
	}

	public function getAction() {
		try{
			$tour_id = $this->_getParam('id');
			if($tour_id != ""){
				$tour_mapper = new Api_Model_TourMapper;
				$tour = $tour_mapper->getTourById($tour_id);

				$this->getresponse()->appendBody(json_encode($tour));
				$this->getResponse()->setHttpResponseCode(200);
			} else{
				throw new Exception(json_encode(array('meta' => 'Bad request')),400);
			}

		}catch(Exception $e){
			$this->getresponse()->appendBody($e->getMessage());
			$this->getResponse()->setHttpResponseCode($e->getCode());
		}
	}

	public function headAction() {

	}

	public function indexAction() {
		try{
			$tour_mapper = new Api_Model_TourMapper();
			$resource = $this->_getParam('param1');

			if($resource == 'location'){
				$location_id = $this->_getParam('param2');
                                
				if ($location_id != NULL){
					$tour = $tour_mapper->getAllLocationTour($location_id);
					$this->getresponse()->appendBody(json_encode($tour));
					$this->getResponse()->setHttpResponseCode(200);

				} else {
					throw new Exception(json_encode(array('meta' => 'Bad request')),400);
				}
			} elseif ($resource == 'user') {
				$user_id = $this->_getParam('param2');
				if ($user_id != NULL){
					$tour = $tour_mapper->getAllUserTour($user_id);
					$this->getresponse()->appendBody(json_encode($tour));
					$this->getResponse()->setHttpResponseCode(200);

				} else {
					throw new Exception(json_encode(array('meta' => 'Bad request')),400);
				}
			}



		}catch(Exception $e){
			$this->getresponse()->appendBody($e->getMessage());
			$this->getResponse()->setHttpResponseCode($e->getCode());
		}
	}

	public function postAction() {
		try{
			$tour_mapper = new Api_Model_TourMapper();
			$rawBody = $this->getRequest()->getRawBody();
			$data = Zend_Json::decode($rawBody);

			if ($data['tour'] != "" || $data['user'] != ""){
				$tour_id = $tour_mapper->createTour($data);
				$this->getresponse()->appendBody(json_encode($tour_id));
				$this->getResponse()->setHttpResponseCode(201);
			} else {
				throw new Exception(json_encode(array('meta' => 'Bad request')),400);
			}

		}catch(Exception $e){
			$this->getresponse()->appendBody($e->getMessage());
			$this->getResponse()->setHttpResponseCode($e->getCode());
		}
	}

	public function inviteAction() {
		try{
			$tour_mapper = new Api_Model_TourMapper();
			$rawBody = $this->getRequest()->getRawBody();
			$data = Zend_Json::decode($rawBody);

			$tour_id = $this->_getParam('param1');

			if ($data != ""){
				$tour_mapper->inviteUser($data, $tour_id);
				throw new Exception(json_encode(array('meta' => 'Created')),201);
			} else {
				throw new Exception(json_encode(array('meta' => 'Bad request')),400);
			}

		}catch(Exception $e){
			$this->getresponse()->appendBody($e->getMessage());
			$this->getResponse()->setHttpResponseCode($e->getCode());
		}
	}

	public function getmemberAction() {
		try{
			$tour_id = $this->_getParam('param1');
			if($tour_id != ""){
				$tour_mapper = new Api_Model_TourMapper;
				$member = $tour_mapper->getTourMember($tour_id);

				$this->getresponse()->appendBody(json_encode($member));
				$this->getResponse()->setHttpResponseCode(200);
			} else{
				throw new Exception(json_encode(array('meta' => 'Bad request')),400);
			}

		}catch(Exception $e){
			$this->getresponse()->appendBody($e->getMessage());
			$this->getResponse()->setHttpResponseCode($e->getCode());
		}
	}

	public function getcheckinAction() {
		try{
			$tour_id = $this->_getParam('param1');
                        var_dump($tour_id);
			if($tour_id != ""){
				$tour_mapper = new Api_Model_TourMapper;
				$loc = $tour_mapper->getCheckInLocation($tour_id);

				$this->getresponse()->appendBody(json_encode($loc));
				$this->getResponse()->setHttpResponseCode(200);
			} else{
				throw new Exception(json_encode(array('meta' => 'Bad request')),400);
			}

		}catch(Exception $e){
			$this->getresponse()->appendBody($e->getMessage());
			$this->getResponse()->setHttpResponseCode($e->getCode());
		}
	}

	public function putAction() {
		try{
			$tour_id = $this->_getParam('id');
			$tour_mapper = new Api_Model_TourMapper();
			if($tour_id != ""){
				$rawBody = $this->getRequest()->getRawBody();
				$data = Zend_Json::decode($rawBody);
				$edit = $tour_mapper->updateTour($data, $tour_id);

				$this->getresponse()->appendBody(json_encode($edit));
				$this->getResponse()->setHttpResponseCode(200);

			} else {
				throw new Exception(json_encode(array('meta' => 'Bad request')),400);
			}
			 
		}catch(Exception $e){
			$this->getresponse()->appendBody($e->getMessage());
			$this->getResponse()->setHttpResponseCode($e->getCode());
		}
	}
        
        public function checkinAction() {
		try{
			$tour_mapper = new Api_Model_TourMapper();
                        $rawBody = $this->getRequest()->getRawBody();
                        $data = Zend_Json::decode($rawBody);
                        
                        if($data['tour_id'] != "" && $data['user_id'] && $data['location_id']){
                                $tour_mapper->checkin($data);
				throw new Exception(json_encode(array('meta' => 'Created')),201);

			} else {
				throw new Exception(json_encode(array('meta' => 'Bad request')),400);
			}
			 
		}catch(Exception $e){
			$this->getresponse()->appendBody($e->getMessage());
			$this->getResponse()->setHttpResponseCode($e->getCode());
		}
	}

	public function checkAction() {
		try{
			$tour_id = $this->_getParam('param1');
			$tour_mapper = new Api_Model_TourMapper();
			if($tour_id != ""){
				$user = $tour_mapper->checkUser($tour_id);

				$this->getresponse()->appendBody(json_encode($user));
				$this->getResponse()->setHttpResponseCode(200);

			} else {
				throw new Exception(json_encode(array('meta' => 'Bad request')),400);
			}
			 
		}catch(Exception $e){
			$this->getresponse()->appendBody($e->getMessage());
			$this->getResponse()->setHttpResponseCode($e->getCode());
		}
	}

	public function joinAction() {
		try{
			$tour_id = $this->_getParam('param1');
			$tour_mapper = new Api_Model_TourMapper();
			$rawBody = $this->getRequest()->getRawBody();
			$data = Zend_Json::decode($rawBody);

			if($tour_id != "" || $data != ""){
				$tour_mapper->joinTour($data, $tour_id);

				throw new Exception(json_encode(array('meta' => 'OK')),200);

			} else {
				throw new Exception(json_encode(array('meta' => 'Bad request')),400);
			}
			 
		}catch(Exception $e){
			$this->getresponse()->appendBody($e->getMessage());
			$this->getResponse()->setHttpResponseCode($e->getCode());
		}
	}
        
        public function outAction() {
		try{
			$tour_id = $this->_getParam('param1');
			$tour_mapper = new Api_Model_TourMapper();
			$rawBody = $this->getRequest()->getRawBody();
			$data = Zend_Json::decode($rawBody);

			if($tour_id != "" || $data != ""){
				$tour_mapper->outTour($data, $tour_id);

				throw new Exception(json_encode(array('meta' => 'OK')),200);

			} else {
				throw new Exception(json_encode(array('meta' => 'Bad request')),400);
			}
			 
		}catch(Exception $e){
			$this->getresponse()->appendBody($e->getMessage());
			$this->getResponse()->setHttpResponseCode($e->getCode());
		}
	}

	public function answerAction() {
		try{
			$tour_id = $this->_getParam('param1');
			$tour_mapper = new Api_Model_TourMapper();
			$rawBody = $this->getRequest()->getRawBody();
			$data = Zend_Json::decode($rawBody);

			if($tour_id != "" || $data != ""){
				$tour_mapper->answer($data, $tour_id);

				throw new Exception(json_encode(array('meta' => 'OK')),200);

			} else {
				throw new Exception(json_encode(array('meta' => 'Bad request')),400);
			}
			 
		}catch(Exception $e){
			$this->getresponse()->appendBody($e->getMessage());
			$this->getResponse()->setHttpResponseCode($e->getCode());
		}
	}
}
